package com.ruoyi.report_lqn.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lqn.domain.bo.ReportLqnRhlqclwjlbBaDetailBo;
import com.ruoyi.report_lqn.domain.vo.ReportLqnRhlqclwjlbBaDetailVo;
import com.ruoyi.report_lqn.domain.ReportLqnRhlqclwjlbBaDetail;
import com.ruoyi.report_lqn.mapper.ReportLqnRhlqclwjlbBaDetailMapper;
import com.ruoyi.report_lqn.service.IReportLqnRhlqclwjlbBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 乳化沥青蒸发残留物含量试验检测记录-详情Service业务层处理
 *
 * @author tll
 * @date 2024-09-12
 */
@RequiredArgsConstructor
@Service
public class ReportLqnRhlqclwjlbBaDetailServiceImpl implements IReportLqnRhlqclwjlbBaDetailService {

    private final ReportLqnRhlqclwjlbBaDetailMapper baseMapper;

    /**
     * 查询乳化沥青蒸发残留物含量试验检测记录-详情
     */
    @Override
    public ReportLqnRhlqclwjlbBaDetailVo queryById(Long detailId){
        return baseMapper.selectVoById(detailId);
    }

    /**
     * 查询乳化沥青蒸发残留物含量试验检测记录-详情列表
     */
    @Override
    public TableDataInfo<ReportLqnRhlqclwjlbBaDetailVo> queryPageList(ReportLqnRhlqclwjlbBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLqnRhlqclwjlbBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportLqnRhlqclwjlbBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询乳化沥青蒸发残留物含量试验检测记录-详情列表
     */
    @Override
    public List<ReportLqnRhlqclwjlbBaDetailVo> queryList(ReportLqnRhlqclwjlbBaDetailBo bo) {
        LambdaQueryWrapper<ReportLqnRhlqclwjlbBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLqnRhlqclwjlbBaDetail> buildQueryWrapper(ReportLqnRhlqclwjlbBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLqnRhlqclwjlbBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportLqnRhlqclwjlbBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportLqnRhlqclwjlbBaDetail::getYangpbh, bo.getYangpbh());
        lqw.eq(bo.getHejzl() != null, ReportLqnRhlqclwjlbBaDetail::getHejzl, bo.getHejzl());
        lqw.eq(bo.getRuyhjzl() != null, ReportLqnRhlqclwjlbBaDetail::getRuyhjzl, bo.getRuyhjzl());
        lqw.eq(bo.getCanlwhjzl() != null, ReportLqnRhlqclwjlbBaDetail::getCanlwhjzl, bo.getCanlwhjzl());
        lqw.eq(bo.getCanlwhl() != null, ReportLqnRhlqclwjlbBaDetail::getCanlwhl, bo.getCanlwhl());
        lqw.eq(bo.getCanlwhlPjz() != null, ReportLqnRhlqclwjlbBaDetail::getCanlwhlPjz, bo.getCanlwhlPjz());
        return lqw;
    }

    /**
     * 新增乳化沥青蒸发残留物含量试验检测记录-详情
     */
    @Override
    public Boolean insertByBo(ReportLqnRhlqclwjlbBaDetailBo bo) {
        ReportLqnRhlqclwjlbBaDetail add = BeanUtil.toBean(bo, ReportLqnRhlqclwjlbBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

    /**
     * 修改乳化沥青蒸发残留物含量试验检测记录-详情
     */
    @Override
    public Boolean updateByBo(ReportLqnRhlqclwjlbBaDetailBo bo) {
        ReportLqnRhlqclwjlbBaDetail update = BeanUtil.toBean(bo, ReportLqnRhlqclwjlbBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLqnRhlqclwjlbBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除乳化沥青蒸发残留物含量试验检测记录-详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
